package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.mapper.ProductStockMapper;
import com.goods.business.service.ProductService;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductStock;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @description：
 * @version:
 * @author：ruixinag
 * @create：Created in 2021/11/8 14:07
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    //物资资料分页列表展示
    @Override
    public  Map<String,Object> findProductList(Map map) {
        String pageNum = (String) map.get("pageNum");
        String pageSize = (String) map.get("pageSize");
        String name = (String) map.get("name");
        String categoryId = (String) map.get("categoryId");
        Object supplier = map.get("supplier");
        String status = (String) map.get("status");
        String categorys = (String) map.get("categorys");


        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        if(name!=null && !"".equals(name)){
            criteria.andLike("name","%"+name+"%");
        }
        criteria.andEqualTo("status",status);
        if(null !=categorys&&!"".equals(categorys)){
            String[] split = categorys.split(",");
            String[] nums=new String[]{"one","two","three"};
            for (int i = 0; i < split.length; i++) {
                criteria.andEqualTo(nums[i]+"CategoryId",split[i]);
            }
        }

        PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        List<Product> products = productMapper.selectByExample(example);
        PageInfo<Product> pageInfo = new PageInfo<>(products);

        //List<ProductVO> productVOList = products.stream().map(product -> {
        //    ProductVO productVO = new ProductVO();
        //    BeanUtils.copyProperties(product, productVO);
        //    return productVO;
        //}).collect(Collectors.toList());

        List<ProductVO> productVOList = pageInfo.getList().stream().map(product -> {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product, productVO);
            return productVO;
        }).collect(Collectors.toList());

        HashMap<String, Object> result = new HashMap<>();
        result.put("rows",productVOList);
        result.put("total",pageInfo.getTotal());


        return result;
    }

    //添加物资信息
    @Override
    public void add(ProductVO productVO) {
        productVO.setCreateTime(new Date());
        productVO.setModifiedTime(new Date());
        productVO.setStatus(2);
        String uuid = UUID.randomUUID().toString();
        productVO.setPNum(uuid);
        Long[] categoryKeys = productVO.getCategoryKeys();
        productVO.setOneCategoryId(categoryKeys[0]);
        productVO.setTwoCategoryId(categoryKeys[1]);
        productVO.setThreeCategoryId(categoryKeys[2]);
        Product product = new Product();
        BeanUtils.copyProperties(productVO,product);
        productMapper.insert(product);
    }

    //审核通过
    @Override
    public void publish(Long id) {
        changeStatus(id,0);
    }

    //回收站,把状态该成2
    @Override
    public void remove(Long id) {
        changeStatus(id,2);
    }

    //恢复,把状态改成0
    @Override
    public void back(Long id) {
        changeStatus(id,0);
    }

    //删除
    @Override
    public void delete(Long id) {
        productMapper.deleteByPrimaryKey(id);
    }

    //编辑,回显数据
    @Override
    public ProductVO edit(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(product, productVO);
        return productVO;
    }

    //更新数据
    @Override
    public void update(Long id, ProductVO productVO) {
        productVO.setModifiedTime(new Date());

        Long[] categoryKeys = productVO.getCategoryKeys();
        productVO.setOneCategoryId(categoryKeys[0]);
        productVO.setTwoCategoryId(categoryKeys[1]);
        productVO.setThreeCategoryId(categoryKeys[2]);
        Product product = new Product();
        BeanUtils.copyProperties(productVO,product);
        productMapper.updateByPrimaryKey(product);
    }

    //获取状态为1的商品
    @Override
    public PageInfo<ProductVO> findProducts(Map paramMap) {
        String pageNum = (String) paramMap.get("pageNum");
        String pageSize = (String) paramMap.get("pageSize");
        String status = (String) paramMap.get("status");

        Example o = new Example(Product.class);
        o.createCriteria().andEqualTo("status",status);

        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        List<Product> products = productMapper.selectByExample(o);
        List<ProductVO> list = products.stream().map(product -> {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product, productVO);
            return productVO;
        }).collect(Collectors.toList());
        PageInfo<ProductVO> pageInfo = new PageInfo<>(list);

        return pageInfo;
    }

    @Autowired
    private ProductStockMapper productStockMapper;
    //返回库存分页信息
    @Override
    public PageInfo<ProductStockVO> findProductStocks(Map paramMap) {
        String pageNum = (String) paramMap.get("pageNum");
        String pageSize = (String) paramMap.get("pageSize");

        String name = (String) paramMap.get("name");
        String categorys = (String) paramMap.get("categorys");
        //构造查询条件
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        if(name!=null && !"".equals(name)){
            criteria.andLike("name","%"+name+"%");
        }
        if(!StringUtils.isEmpty(categorys)){
            String[] split = categorys.split(",");
            String[] nums=new String[]{"one","two","three"};
            for (int i = 0; i < split.length; i++) {
                criteria.andEqualTo(nums[i]+"CategoryId",split[i]);
            }
        }


        PageHelper.startPage(Integer.parseInt(pageNum.trim()),Integer.parseInt(pageSize.trim()));

        List<Product> products = productMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo<>(products);

        List<ProductStockVO> list = products.stream().map(product -> {
            ProductStockVO productStockVO = new ProductStockVO();
            BeanUtils.copyProperties(product, productStockVO);
            //查库存
            Example o = new Example(ProductStock.class);
            o.createCriteria().andEqualTo("pNum", product.getPNum());
            ProductStock productStock = productStockMapper.selectOneByExample(o);
            if(null != productStock){
                productStockVO.setStock(productStock.getStock());
            }else {
                productStockVO.setStock(0L);
            }

            return productStockVO;
        }).collect(Collectors.toList());
        pageInfo.setList(list);

        return pageInfo;
    }


    //改变物资的状态
    private void changeStatus(Long id,int status){
        Product product = productMapper.selectByPrimaryKey(id);
        product.setStatus(status);
        productMapper.updateByPrimaryKey(product);
    }






}
